﻿using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            PopulateTreeView();
            FilesListView.MouseDoubleClick += FilesListView_MouseDoubleClick;
        }

        private void PopulateTreeView()
        {
            var drives = Directory.GetLogicalDrives();
            foreach (var drive in drives)
            {
                TreeViewItem item = new TreeViewItem
                {
                    Header = drive,
                    Tag = drive
                };
                item.Items.Add(null);
                item.Expanded += Folder_Expanded;
                item.Selected += Folder_Selected;
                FoldersTreeView.Items.Add(item);
            }
        }

        private void Folder_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            if (item.Items.Count == 1 && item.Items[0] == null)
            {
                item.Items.Clear();
                try
                {
                    foreach (var directory in Directory.GetDirectories(item.Tag.ToString()))
                    {
                        TreeViewItem subItem = new TreeViewItem
                        {
                            Header = Path.GetFileName(directory),
                            Tag = directory
                        };
                        subItem.Items.Add(null);
                        subItem.Expanded += Folder_Expanded;
                        subItem.Selected += Folder_Selected;
                        item.Items.Add(subItem);
                    }
                }
                catch { }
            }
        }

        private void Folder_Selected(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            if (item != null)
            {
                PopulateFiles(item.Tag.ToString());
                e.Handled = true;  // 防止事件继续向上传递
            }
        }

        private void PopulateFiles(string path)
        {
            FilesListView.Items.Clear();
            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(path);
                foreach (var folder in dirInfo.GetDirectories())
                {
                    FilesListView.Items.Add(new { Name = "Folder: " + folder.Name, Path = folder.FullName });
                }
                foreach (var file in dirInfo.GetFiles())
                {
                    FilesListView.Items.Add(new { Name = "File: " + file.Name, Path = file.FullName });
                }
            }
            catch { }
        }

        private void FilesListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (FilesListView.SelectedItem != null)
            {
                dynamic selected = FilesListView.SelectedItem;
                string selectedPath = selected.Path;
                if (File.Exists(selectedPath) && Path.GetExtension(selectedPath) == ".txt")
                {
                    System.Diagnostics.Process.Start("notepad.exe", selectedPath);
                }
            }
        }
    }
}